package Lambdademo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-08-02
 * Time: 13:53
 */



//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn {
    void test();
}


//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}

//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}


//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}


//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}

//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}


public class LambdaTest {

    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("hello", 3);
        map.put("mak",2);
        map.put("haha",5);

        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key: "+ s + "val: "+integer);
            }
        });

        System.out.println();
        System.out.println("====Lambda表达式写法====");
        map.forEach((k,v)-> System.out.println("key: "+ k + "val: "+v));

    }

    public static void main8(String[] args) {
        ArrayList<String> list = new ArrayList<>();

        list.add("hello");
        list.add("my");
        list.add("nameIs...");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        System.out.println();
        System.out.println("====Lambda表达式写法====");
        list.forEach(s -> System.out.println(s));



        //sort方法
        list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });

        System.out.println("====Lambda表达式写法====");
        list.sort((o1, o2) -> o1.compareTo(o2));
        System.out.println(list);

    }

    public static void main7(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        //Lambda表达式写法
        PriorityQueue<Integer> priorityQueue1 = new PriorityQueue<>((o1, o2) -> o1.compareTo(o2));

    }

    public static void main6(String[] args) {
        MoreParameterReturn moreParameterReturn = (a,b) -> a+b;
        System.out.println(moreParameterReturn.test(2,4));
    }

    public static void main5(String[] args) {
        OneParameterReturn oneParameterReturn = (int a) ->a+1;
        System.out.println(oneParameterReturn.test(40));
    }

    public static void main4(String[] args) {
//        NoParameterReturn noParameterReturn = ()-> {return 23;};

        NoParameterReturn noParameterReturn = ()-> 10;
        System.out.println(noParameterReturn.test());
    }


    public static void main3(String[] args) {
        MoreParameterNoReturn moreParameterNoReturn = (a,b) -> System.out.println(a+b);
        moreParameterNoReturn.test(1,3);
    }

    public static void main2(String[] args) {
        /*OneParameterNoReturn oneParameterNoReturn = (int a)->{
            System.out.println(a+1);
        };*/

        OneParameterNoReturn oneParameterNoReturn =  a-> System.out.println(a+1);
        oneParameterNoReturn.test(12);

    }

    public static void main1(String[] args) {
        //匿名内部类方式
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn() {
            @Override
            public void test() {
                System.out.println("重写了test方法");
            }
        };

        noParameterNoReturn.test();
        System.out.println("==========");

        //Lambda表达式方式：
        NoParameterNoReturn noParameterNoReturn1 = () -> {
            System.out.println("重写了test方法");};
        noParameterNoReturn1.test();
    }
}
